package com.dong.route;

import org.springframework.context.annotation.Bean;

import com.dong.filter.ElapsedFilter;
import com.dong.filter.TokenFilter;
import com.dong.filter.factory.ElapsedGatewayFilterFactory;
import com.dong.filter.ratelimit.RemoteAddrKeyResolver;
//路由有两种方式配置,一种是配置文件application的方式,一种是代码配置  下边是代码配置，application 中也进行了配置
//@Configuration
public class Route {
//    @Bean
//    public RouteLocator customerRouteLocator(RouteLocatorBuilder builder) {
//        // @formatter:off
//        return builder.routes()
//                .route(
//                        r -> r.path("/fluent/customer/**")
//                             .filters(f -> f.stripPrefix(2)
//                                            .filter(new ElapsedFilter())
//                                            .addResponseHeader("X-Response-Default-Foo", "Default-Bar"))
//                             .uri("lb://CONSUMER")
//                             .order(0)
//                             .id("fluent_customer_service")
//                )
//                .route(r -> r.path("/throttle/customer/**")
//                             .filters(f -> f.stripPrefix(2)
//                                            .filter(new RateLimitByIpGatewayFilter(10, 1, Duration.ofSeconds(1)))
//                                            .filter(rateLimitByCpuGatewayFilter))
//                             .uri("lb://CONSUMER")
//                             .order(0)
//                             .id("throttle_customer_service")
//                )
//                .build();
//        // @formatter:on
//    }

//    @Autowired
//    private RateLimitByCpuGatewayFilter rateLimitByCpuGatewayFilter;

    @Bean
    public ElapsedGatewayFilterFactory elapsedGatewayFilterFactory() {
        return new ElapsedGatewayFilterFactory();
    }

    @Bean
    public TokenFilter tokenFilter() {
        return new TokenFilter();
    }

    @Bean
    public ElapsedFilter elapsedFilter(){
        return new ElapsedFilter();
    }

    @Bean(name = RemoteAddrKeyResolver.BEAN_NAME)
    public RemoteAddrKeyResolver remoteAddrKeyResolver() {
        return new RemoteAddrKeyResolver();
    }

}
